package cj.web.marry.controllers;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.paoding.rose.web.annotation.Param;
import net.paoding.rose.web.annotation.Path;
import net.paoding.rose.web.annotation.rest.Get;
import net.paoding.rose.web.var.Model;
import net.sf.json.JSONObject;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;

import cj.utils.Constants;
import cj.utils.DateUtils;
import cj.utils.HttpClientUtil;
import cj.utils.weixin.HttpsWenXinUtil;
import cj.web.marry.dao.UserApplicationDAO;
import cj.web.marry.dao.VideosDAO;

@Path("videos")
public class VideosController {
	
	protected static final Log logger = LogFactory.getLog(VideosController.class); 
	
	public final static String marryCode="999";
	public final static String image=".jpg";
	public final static String video=".mp4";
	public final static String videoUrl="http://121.40.251.139/marriage/";
	public final static String imgUrl="http://marriage-i.haoyicn.cn/marriage/";

	@Autowired
	private VideosDAO videosDAO;
	
	@Autowired
	private UserApplicationDAO userApplicationDAO;
	
	//我的视频
	@Get("myVideoDetail")
	public String myVideoDetail(Model model, @Param("code")String code)
	{
		String openId = HttpsWenXinUtil.retOpenId(code,Constants.weixinkey.AppID,Constants.weixinkey.AppSecret);
		return "r:myVideoDetailByOpenId?openId="+openId;
	}
	
	/**
	 * 我的视频ByOpenId
	 * @param model
	 * @param openId
	 * @return
	 */
	@Get("myVideoDetailByOpenId")
	public String myVideoDetailByOpenId(Model model, @Param("openId")String openId)
	{
		List<Map> reps = userApplicationDAO.queryByOpenId(openId);
		
		if(reps!=null && reps.size()>0)
		{
			model.add("id", reps.get(0).get("id"));
			model.add("openid", reps.get(0).get("openid"));
			model.add("nickname", reps.get(0).get("nickname"));
			model.add("video", reps.get(0).get("id")+video);
			model.add("image", reps.get(0).get("id")+image);
			boolean flag = HttpClientUtil.isConnect(videoUrl+reps.get(0).get("id")+video);
			if(flag)
			{
				return "/views/marry/myVideoDetail.vm";
			}
			else
			{
				return "r:empty?typeCode=1";
			}
		}
		else
		{
			return "r:empty?typeCode=1";
		}
	}
	
	//我的号外
	@Get("myImageDetail")
	public String myImageDetail(Model model, @Param("code")String code)
	{
		String openId = HttpsWenXinUtil.retOpenId(code,Constants.weixinkey.AppID,Constants.weixinkey.AppSecret);
		return "r:myImageDetailByOpenId?openId="+openId;
	}
	
	/**
	 * 我的号外ByOpenId查询
	 * @param model
	 * @param openId
	 * @return
	 * @throws IOException 
	 * @throws HttpException 
	 */
	@Get("myImageDetailByOpenId")
	public String myImageDetailByOpenId(Model model, @Param("openId")String openId) throws HttpException, IOException
	{
		List<Map> reps = userApplicationDAO.queryByOpenId(openId);
		
		if(reps!=null && reps.size()>0)
		{
			//幸福码
			String code = reps.get(0).get("id").toString();
			//号外
			String hw = "s_"+reps.get(0).get("id")+image;
			
			model.add("id", reps.get(0).get("id"));
			model.add("openid", reps.get(0).get("openid"));
			model.add("nickname", reps.get(0).get("nickname"));
			
			HttpClient client = new HttpClient();
	        HttpMethod method = new GetMethod(imgUrl+hw);
	        int i=client.executeMethod(method);
	        // 打印服务器返回的状态
	        if(method.getStatusCode()==200)
	        {
	        	model.add("photo", hw);
	        }
	        else
	        {
	        	int size = videosDAO.queryMarriagePhotoRefSize(code);
	        	if(size>0){
	        		List<Map> rep = videosDAO.queryMarriagePhotoReflis(code);
	        		model.add("photo", rep.get(0).get("photo"));
	        	}else{
	        		model.add("photo", "temp.jpg");
	        	}
	        }
			
		}
		else
		{
			return "r:empty?typeCode=2";
		}
		
		return "/views/marry/myImageDetail.vm";
	}
	
	@Get("videoLis")
	public String videoLisByCode(Model model, @Param("code")String code)
	{
		String openId = HttpsWenXinUtil.retOpenId(code,Constants.weixinkey.AppID,Constants.weixinkey.AppSecret);
//		String openId = "ofwuNswvO5usQiL5qfjIowAVykOU";
		
		return "r:videoLisForOpenId?openId="+openId;
	}
	
	//视频秀
	@Get("videoLisForOpenId")
	public String videoLisForOpenId(Model model,@Param("openId")String openId)
	{
		model.add("openId",openId);
		//视频秀Size
		int totalSize=videosDAO.queryMarriageVideosByTitleSize(marryCode);
		if(totalSize==0)
		{
			return "r:empty?typeCode=3";
		}
		
		return "/views/marry/videoLis.vm";
	}
	
	@Get("empty")
	public String success(Model model, @Param("typeCode")String typeCode){
		model.add("typeCode", typeCode);
		if(typeCode.equals("1"))
		{
			model.add("video", "xcp"+video);
			model.add("image", "176/4371461902682176"+image);
			return "/views/marry/DemoVideoDetail.vm";
		}
		return "/views/marry/empty.vm";
	}
	
	/**
	 * 视频秀
	 * @param type
	 * putong("普通", 1),
	 * teshe("特色", 2);
	 * @param start
	 * @param length
	 * @return
	 */
	@Get("queryVideoLis")
	public String queryVideoLis(@Param("start") int start,@Param("length") int length)
	{
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		//视频秀列表
		List<Map> reps = videosDAO.queryMarriageVideosByTitle(marryCode, start, length);
		//视频秀Size
		int totalSize=videosDAO.queryMarriageVideosByTitleSize(marryCode);
		
		List<Map> videosRepsList = new ArrayList<Map>();
		
		for(Map rep :reps)
		{
			Map videoRep = new HashMap();
			videoRep.put("id", rep.get("id"));
			videoRep.put("title", rep.get("title"));
//			videoRep.put("image", rep.get("image"));
			
			//缩率图
			String carouselImage = rep.get("image")==null?"":rep.get("image").toString();
			if(carouselImage !=null && !"".equals(carouselImage))
			{
				String carouselImages[] = carouselImage.split("/");
				videoRep.put("image", carouselImages[0]+"/s_"+carouselImages[1]);//缩率图
			}
			else
			{
				videoRep.put("image", "");//缩率图
			}
			
			videoRep.put("video", rep.get("video"));
			videoRep.put("type", rep.get("type"));
			videoRep.put("man", rep.get("man"));
			videoRep.put("manCard", rep.get("manCard"));
			videoRep.put("woman", rep.get("woman"));
			videoRep.put("womanCard", rep.get("womanCard"));
			
			videosRepsList.add(videoRep);
		}
		
		jo.put("objs", videosRepsList);
		jo.put("total", totalSize);
		return "@"+jo.toString();
	}
	
	//视频秀Detail
	@Get("videoDetail")
	public String videoDetail(Model model, @Param("id")int id)
	{
		List<Map> reps = videosDAO.queryMarriageVideosById(id);
		
		if(reps!=null && reps.size()>0)
		{
			model.add("id", reps.get(0).get("id"));
			model.add("title", reps.get(0).get("title"));
//			model.add("image", reps.get(0).get("image"));
			
			//缩率图
			String carouselImage = reps.get(0).get("image")==null?"":reps.get(0).get("image").toString();
			if(carouselImage !=null && !"".equals(carouselImage))
			{
				String carouselImages[] = carouselImage.split("/");
				model.add("image", carouselImages[0]+"/s_"+carouselImages[1]);//缩率图
			}
			else
			{
				model.add("image", "");//缩率图
			}
			
			model.add("video", reps.get(0).get("video"));
			model.add("type", reps.get(0).get("type"));
			model.add("man", reps.get(0).get("man"));
			model.add("manCard", reps.get(0).get("manCard"));
			model.add("woman", reps.get(0).get("woman"));
			model.add("womanCard", reps.get(0).get("womanCard"));
		}
		
		return "/views/marry/videoDetail.vm";
	}
}
